﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpPTCLib;
using System.Drawing;

namespace SharpPTCSamples
{
    class Fire
    {
        static SharpPTCLib.SharpPTC ptc;
        static void Main(string[] args)
        {
            ptc = new SharpPTC();
            ptc.StartAll("Fire demo", 320, 240, 2, false);
            ptc.SetClsColor(Color.Black);

            Random rand = new Random();
            int[] buffer = new int[ptc.Width * (ptc.Height + 10)];
            int[] palette = new int[257];

            //Create palette
            int r = 0;
            int g = 0;
            for (int k = 0; k < 256; k++)
            {
                palette[k] = ptc.RGBToInt32(r, g, 0);
                r++;
                if (r > 128)
                {
                    g++;
                }
            }
            //Set the last palette entry to be white
            palette[256] = ptc.ColorToInt32(Color.White);

            //Main loop
            do
            {
                //Randomize bottom lines
                for (int i = 0; i < ptc.Width; i++)
                {
                    int color = rand.Next(128, 256);
                    ptc.SetDrawColor(palette[color]);
                    if (rand.Next(0, 10) == 5)
                    {
                        for (int k = 0; k < 2; k++)
                        {
                            buffer[(((ptc.Height - 1) + k) * ptc.Width) + i] = 256;
                            buffer[(((ptc.Height - 1) + k) * ptc.Width) + (i + 1)] = 256;
                        }
                    }
                    else
                    {
                        buffer[((ptc.Height - 1) * ptc.Width) + i] = color;
                        buffer[((ptc.Height - 1) * ptc.Width) + i] = color;
                    }
                }

                //Blur
                for (int y = 2; y < ptc.Height; y++)
                {
                    for (int x = 0; x < ptc.Width; x++)
                    {
                        int color1 = buffer[(ptc.Width * y) + (x - 1)];
                        int color2 = buffer[(ptc.Width * y) + (x + 1)];
                        int color3 = buffer[(ptc.Width * (y - 1)) + x];
                        int color4 = buffer[(ptc.Width * (y + 1)) + x];
                        int color5 = buffer[(ptc.Width * (y + 1)) + (x - 1)];
                        int color6 = buffer[(ptc.Width * (y + 1)) + (x + 1)];
                        int color7 = buffer[(ptc.Width * (y - 1)) + (x - 1)];
                        int color8 = buffer[(ptc.Width * (y - 1)) + (x + 1)];
                        int final = (color1 + color2 + color3 + color4 + color5 + color6 + color7 + color8) / 8 - 1;
                        if (final < 0) final = 0;
                        ptc.pixel[(ptc.Width * (y - 1)) + x] = palette[final];
                        buffer[(ptc.Width * (y - 1)) + x] = final;
                    }
                }

            } while (ptc.UpdateAll());
        }
    }
}
